﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheHeGeo.Util;
using TheHeGeoDao;

namespace TheHeGeo.UserControl
{
    public partial class UcEqManagementEnglish : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session[SessionConst.SES_GRV_LOOKUP_DATA] = null;
                Session[SessionConst.SES_GRV_LOOKUP_DA] = null;
                LoadGrluDa();
            }
            else
            {
                grdLook.DataSource = Session[SessionConst.SES_GRV_LOOKUP_DATA];
                grdDa.DataSource = Session[SessionConst.SES_GRV_LOOKUP_DA];

            }

            if (Request.QueryString["r"] != null && Request.QueryString["n"] != null&&Request.QueryString["d"]!=null)
            {
                int maDa;
                if(!int.TryParse(Request.QueryString["d"],out maDa)) return;
                grdDa.Value = maDa;
                LoadGrlu(maDa);
                divContent.Controls.Clear();
                var str = Request.QueryString["r"].TrimEnd(';');
                if (string.IsNullOrEmpty(str)) return;
               
                
                var uc = (UcJsChartEnglish) Page.LoadControl("~/UserControl/UcJsChartEnglish.ascx");
                uc.QueryString = Request.QueryString["r"];
                uc.SensorName = Request.QueryString["n"].Split(',');
                uc.MaDuAn = maDa;
                divContent.Style["min-height"] = "0";
                divContent.Controls.Add(uc);
            }
            else
            {
                divContent.Controls.Clear();
                divContent.Style["min-height"] = "750px";

            }
        }

        protected void LoadGrluDa()
        {
            var db = Session[SessionConst.SES_GRV_LOOKUP_DA];
            if (db == null)
            {
                var us = ((SessionUser)Session[SessionConst.SES_USER]).NguoiSuDung.UserName;

                db = (from q in DaoHelper.Entities.PhanQuyenDuAns 
                      join p in DaoHelper.Entities.DuAns on q.MaDuAn equals p.MaDuAn
                      where (q.UserName.Equals(us))
                      select p
                      ).ToList();
                Session[SessionConst.SES_GRV_LOOKUP_DA] = db;
            }


           grdDa.DataSource = db;
           grdDa.DataBind();
        }

        protected void LoadGrlu(int maDa)
        {
            var db = Session[SessionConst.SES_GRV_LOOKUP_DATA];
            if (db==null)
            {
                var us = ((SessionUser)Session[SessionConst.SES_USER]).NguoiSuDung.UserName;

                db = (from s in DaoHelper.Entities.Sensors
                        join t in DaoHelper.Entities.ThietBis on s.MaThietBi equals t.MaThietBi
                        join q in DaoHelper.Entities.PhanQuyenDuAns on t.MaDuAn equals q.MaDuAn
                        where (q.UserName.Equals(us)&&q.MaDuAn==(maDa)&&t.Status==0)
                        select new
                        {
                            s.TenSensor,
                            s.MaSensor,
                            s.MoTa,
                            t.TenThietBi,
                            t.MaThietBi
                        }).ToList();
                Session[SessionConst.SES_GRV_LOOKUP_DATA] = db;
            }


            grdLook.DataSource = db;
            grdLook.DataBind();
        }

        protected void btOk_Click(object sender, EventArgs e)
        {
            GenGrid();
        }

        protected void GenGrid()
        {
            var str = grdLook.Text.Trim();
            if (str.Length > 0)
            {
                if (str.Split(',').Length < 20)
                {
                    hfSensorNames.Value = str;
                    string ma = grdLook.GridView.GetSelectedFieldValues("MaSensor").Aggregate("", (current, o) => current + o + ",");
                    ma = ma.TrimEnd(',');
                    for (int i = 0; i < 20; i++)
                    {
                        Session[SessionConst.SES_CHART_DATA + i] = null;
                    }
                    Response.Redirect("~/Equipments/EquipmentMornitoring_English.aspx?r=" + ma + "&n=" + str+"&d="+grdDa.Value);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, GetType(), "alert10",
                    "<script type='text/javascript'>alert('You can selecet maximum 20 sensors at the same time')</script>", false);
                }
            }
        }

        protected void grdDa_ValueChanged(object sender, EventArgs e)
        {
            if (grdDa.Value != null)
            {
                if (Request.QueryString["r"] != null && Request.QueryString["n"] != null &&
                    Request.QueryString["d"] != null)
                {
                    Response.Redirect("~/Equipments/EquipmentMornitoring_English.aspx?r=;&n=;&d=" + grdDa.Value);
                }
                
                Session[SessionConst.SES_GRV_LOOKUP_DATA] = null;
                LoadGrlu(Convert.ToInt32(grdDa.Value));
                
            }
        }
    }
}